#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
using namespace std;

class Solution {
public:
    bool visited[300][300] = { 0 };
    int ret = 0;
    void dfs(vector<vector<char>>& grid, int i, int j)
    {
        int dics[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
        for (auto dic : dics)
        {
            int x = i + dic[0], y = j + dic[1];
            if (x >= 0 && x < grid.size() && y >= 0 && y < grid[0].size() && !visited[x][y] && grid[x][y] == '1')
            {
                visited[x][y] = true;
                dfs(grid, x, y);
            }
        }
    }

    int numIslands(vector<vector<char>>& grid)
    {
        for (int i = 0; i < grid.size(); ++i)
        {
            for (int j = 0; j < grid[0].size(); ++j)
            {
                if (grid[i][j] == '1' && !visited[i][j])
                {
                    ++ret;
                    visited[i][j] = true;
                    dfs(grid, i, j);
                }
            }
        }
        return ret;
    }

    bool visited[51][51] = { 0 };
    int ret = 0;
    int count;
    void _dfs(vector<vector<int>>& grid, int i, int j)
    {
        ++count;
        visited[i][j] = true;
        int dics[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
        for (auto dic : dics)
        {
            int x = i + dic[0], y = j + dic[1];
            if (x >= 0 && x < grid.size() && y >= 0 && y < grid[0].size() && !visited[x][y] && grid[x][y] == 1)
            {
                _dfs(grid, x, y);
            }
        }
    }

    int maxAreaOfIsland(vector<vector<int>>& grid) {
        for (int i = 0; i < grid.size(); ++i)
        {
            for (int j = 0; j < grid[0].size(); ++j)
            {
                if (grid[i][j] == 1 && !visited[i][j])
                {
                    count = 0;
                    _dfs(grid, i, j);
                    ret = max(ret, count);
                }
            }
        }
        return ret;
    }

    void dfs(vector<vector<char>>& grid, int i, int j)
    {
        grid[i][j] = '.';
        int dics[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
        for (auto dic : dics)
        {
            int x = i + dic[0], y = j + dic[1];
            if (x >= 0 && x < grid.size() && y >= 0 && y < grid[0].size() && grid[x][y] == 'O')
            {
                dfs(grid, x, y);
            }
        }
    }

    void solve(vector<vector<char>>& board) {
        int m = board.size(), n = board[0].size();
        for (int i = 0; i < m; ++i)
        {
            if (board[i][0] == 'O') dfs(board, i, 0);
            if (board[i][n - 1] == 'O') dfs(board, i, n - 1);
        }
        for (int i = 0; i < n; ++i)
        {
            if (board[0][i] == 'O') dfs(board, 0, i);
            if (board[m - 1][i] == 'O') dfs(board, m - 1, i);
        }

        for (int i = 0; i < m; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                if (board[i][j] == '.') board[i][j] = 'O';
                else if (board[i][j] == 'O') board[i][j] = 'X';

            }
        }
    }

    int strStr(string haystack, string needle) {
        int m = haystack.size(), n = needle.size();
        vector<int> next(n);
        for (int i = 1, j = 0; i < n; ++i)
        {
            while (j && needle[i] != needle[j]) j = next[j - 1];
            if (needle[i] == needle[j]) ++j;
            next[i] = j;
        }
        int i = 0, j = 0;
        for (int i = 0, j = 0; i < m; ++i)
        {
            while (j && haystack[i] != needle[j]) j = next[j - 1];
            if (haystack[i] == needle[j]) ++j;
            if (j == n) return i - n + 1;
        }
        return -1;
    }

    bool isMatch(string s, string p) {
        int m = s.size(), n = p.size();
        vector<vector<int>> dp(m + 1, vector<int>(n + 1));
        dp[0][0] = true;
        for (int j = 1; j <= n; ++j)
        {
            if (p[j - 1] == '*') dp[0][j] = true;
            else break;
        }
        for (int i = 1; i <= m; ++i)
        {
            for (int j = 1; j <= n; ++j)
            {
                if (p[j - 1] == '*') dp[i][j] = dp[i - 1][j] | dp[i][j - 1];
                else if (p[j - 1] == '?' || s[i - 1] == p[j - 1])dp[i][j] = dp[i - 1][j - 1];
            }
        }
        return dp[m][n];
    }
};